package com.leyou.cart.service;

import com.leyou.auth.pojo.UserInfo;
import com.leyou.cart.filter.LoginInterceptor;
import com.leyou.cart.pojo.Cart;
import com.leyou.common.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Author: Saka
 * Date: 2019/2/25
 * Time: 10:47
 */
@Service
public class CartService {

    @Autowired
    private StringRedisTemplate redisTemplate;

    static final String KEY_PREFIX = "ly:cart:uid:";


    public void addCart(Cart cart) {
        UserInfo loginUser = LoginInterceptor.getLoginUser();

        BoundHashOperations<String, Object, Object> ops = redisTemplate.boundHashOps(KEY_PREFIX + loginUser.getUsername());

        String skuId = cart.getSkuId().toString();

        Boolean boo = ops.hasKey(skuId);


        if (boo) {//存在,先取出后再修改

            String jsonCart = (String) ops.get(skuId);
            Cart storeCart = JsonUtils.parse(jsonCart, Cart.class);
            storeCart.setNum(storeCart.getNum() + cart.getNum());
            ops.put(skuId, JsonUtils.serialize(storeCart));

        } else {//不存在直接添加

            cart.setUserId(loginUser.getId());

            ops.put(skuId, JsonUtils.serialize(cart));

        }
    }

    public void deleteCart(Long skuId) {
        UserInfo loginUser = LoginInterceptor.getLoginUser();

        BoundHashOperations<String, Object, Object> ops = redisTemplate.boundHashOps(KEY_PREFIX + loginUser.getUsername());

        ops.delete(skuId.toString());
    }

    public List<Cart> queryCart() {

        UserInfo loginUser = LoginInterceptor.getLoginUser();

        BoundHashOperations<String, Object, Object> ops = redisTemplate.boundHashOps(KEY_PREFIX + loginUser.getUsername());

        List<Cart> carts = new ArrayList<>();

        List<Object> values = ops.values();

        values.forEach(value->{
            String jsonValue = (String) value;
            Cart cart = JsonUtils.parse(jsonValue, Cart.class);
            carts.add(cart);

        });

        return carts;
    }

    public void updateNum(Long skuId, Integer num) {
        //获取用户登录信息
        UserInfo loginUser = LoginInterceptor.getLoginUser();

        BoundHashOperations<String, Object, Object> ops = redisTemplate.boundHashOps(KEY_PREFIX + loginUser.getUsername());
        //获取购物车
        String json = ops.get(skuId.toString()).toString();
        Cart parse = JsonUtils.parse(json, Cart.class);
        parse.setNum(num);
        //写入购物车
        ops.put(skuId.toString(), JsonUtils.serialize(parse));
    }

    public void addCarts(List<Cart> carts) {
        UserInfo loginUser = LoginInterceptor.getLoginUser();

        BoundHashOperations<String, Object, Object> ops = redisTemplate.boundHashOps(KEY_PREFIX + loginUser.getUsername());

        for (Cart cart : carts) {
            String skuId = cart.getSkuId().toString();

            Boolean boo = ops.hasKey(skuId);
            if (boo) {//存在,先取出后再修改

                String jsonCart = (String) ops.get(skuId);
                Cart storeCart = JsonUtils.parse(jsonCart, Cart.class);
                storeCart.setNum(storeCart.getNum() + cart.getNum());
                ops.put(skuId, JsonUtils.serialize(storeCart));

            } else {//不存在直接添加

                cart.setUserId(loginUser.getId());

                ops.put(skuId, JsonUtils.serialize(cart));

            }
        }




    }
}
